package com.example.myap;

import android.content.Context;

import com.caimuhao.rxpicker.RxPicker;
import com.example.myap.Utils.Constant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpHeaders;
import com.sunnybear.framework.library.base.BaseApplication;
import com.sunnybear.framework.library.glide.GlideImageLoader;
import com.sunnybear.framework.tools.PreferenceHelper;
import com.sunnybear.framework.tools.log.Logger;
import com.zxy.tiny.Tiny;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import androidx.appcompat.app.AppCompatDelegate;
import okhttp3.OkHttpClient;

/**
 * <p>
 *  2018/3/12.
 */
public class MainApplication extends BaseApplication {

    private static final String TAG = "MainApplication";
    private static Context mContext;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        String localNightMode = PreferenceHelper.getValue("LocalNightMode", "close");
        Logger.debug("localNightMode:-----" + localNightMode);
        AppCompatDelegate.setDefaultNightMode(("close".equals(localNightMode)) ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES);

        /*=========初始化OkGo=============*/
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);


        builder.connectTimeout(Constant.REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
        builder.readTimeout(Constant.REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
        builder.writeTimeout(Constant.REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));

        OkGo go = OkGo.getInstance().init(this).setOkHttpClient(builder.build());
        HttpHeaders headers = new HttpHeaders();
        String token = PreferenceHelper.getValue("token", null);
        headers.put("token", token);
        go.addCommonHeaders(headers);


        Tiny.getInstance().init(this);
        RxPicker.init(new GlideImageLoader());
    }

    public static Context getContext() {
        return mContext;
    }

}